﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.ApplicationServer.Caching;

namespace MDCAdminTool.Controls.Data_Cache.ViewModels
{
    public class SearchDataCacheViewModel : INotifyPropertyChanged
    {
        #region Cache
        private MdcCache _cache;
        public MdcCache Cache
        {
            get { return _cache; }
            set { _cache = value; OnPropertyChanged("Cache"); }
        } 
        #endregion

        #region Key
        private string _key;
        public string Key
        {
            get { return _key; }
            set { _key = value; OnPropertyChanged("Key"); }
        } 
        #endregion

        #region Tags
        private string _tags;
        public string Tags
        {
            get { return _tags; }
            set { _tags = value; OnPropertyChanged("Tags"); }
        } 
        #endregion

        #region SearchResult
        private ObservableCollection<DataCacheItem> _searchResult;
        public ObservableCollection<DataCacheItem> SearchResult
        {
            get { return _searchResult; }
            set { _searchResult = value; OnPropertyChanged("SearchResult"); }
        } 
        #endregion

        public SearchDataCacheViewModel()
        {
            SearchResult = new ObservableCollection<DataCacheItem>();
        }

        public void OnSearch()
        {
            if (Cache == null || string.IsNullOrEmpty(Key))
                return;

            var client = CacheClientFactory.GetCache(Cache);

            var result = client.GetCacheItem(Key);

            if (result != null)
            {
                SearchResult.Clear();
                SearchResult.Add( result );
            }
                
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        } 
        #endregion
    }
}
